package org.edu.modules.oa.controller;

import groovy.util.logging.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.oa.entity.VeOaCarSetEntity;
import org.edu.modules.oa.entity.VeOaMeetPlaceEntity;
import org.edu.modules.oa.service.IVeOaMeetPlaceService;
import org.edu.modules.oa.vo.VeOaCarSetVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Api(tags="会议场地管理")
@RestController
@RequestMapping("oa/VeOaMeetPlace")
@Slf4j
public class VeOaMeetPlaceController {
    @Resource
    private IVeOaMeetPlaceService iVeOaMeetPlaceService;

    // 日志注解
    @AutoLog(value = "会议场地管理-显示表格")
    @ApiOperation(value="显示表格", notes="会议场地管理-显示表格")
    // 显示表格
    @PostMapping(value = "/showTable")

    public Result<?> showTable(VeOaMeetPlaceEntity veOaMeetPlaceEntity, Integer startPage, Integer pageSize) {
        HashMap<String, Object> map=iVeOaMeetPlaceService.showTable(veOaMeetPlaceEntity,startPage,pageSize);

        return Result.OK(map);
    }
    // 批量删除
    @AutoLog(value = "会议场地管理-批量删除")
    @ApiOperation(value="批量删除", notes="会议场地管理-批量删除")
    @PostMapping(value = "/allDelete")

    public Result<?> allDelete(String idStr) {
        Integer result=0;
        String data[] = idStr.split(",");

        for (int i=1;i<data.length;i++){

            result+=iVeOaMeetPlaceService.deleteRecord(Integer.valueOf(data[i]));

        }
        if(result == 0){
            return Result.error(500,"删除失败了");
        }
        return Result.OK();
    }
    // 改变状态
    @AutoLog(value = "会议场地管理-改变状态")
    @ApiOperation(value="改变状态", notes="会议场地管理-改变状态")
    @PostMapping(value = "/alterState")

    public Result<?> alterState(String idStr,Integer state) {
        Integer result=0;
        String data[] = idStr.split(",");

        for (int i=1;i<data.length;i++){

            result+=iVeOaMeetPlaceService.alterState(Long.valueOf(data[i]),state);

        }
        return Result.OK();
    }
    // 单选删除
    @AutoLog(value = "会议场地管理-单选删除")
    @ApiOperation(value="单选删除", notes="会议场地管理-单选删除")
    @PostMapping(value = "/oneDelete")

    public Result<?> oneDelete(Integer id) {


          int result=iVeOaMeetPlaceService.deleteRecord(id);

        if(result == 0){
            return Result.error(500,"删除失败了");
        }
        return Result.OK();
    }
    // 增加场地
    @AutoLog(value = "会议场地管理-增加场地")
    @ApiOperation(value="增加场地", notes="会议场地管理-增加场地")
    @PostMapping(value = "/addPlace")

    public Result<?> addPlace(VeOaMeetPlaceEntity veOaMeetPlaceEntity) {
        // 重复判断
        VeOaMeetPlaceEntity entity= iVeOaMeetPlaceService.epetitionrJudge(veOaMeetPlaceEntity.getPlaceName());
        if(entity!=null){

            return  Result.error(500,"会议场地重复！");

        }
        // 获取redis存的用户值

        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        veOaMeetPlaceEntity.setCreateBy(user.getId());
        veOaMeetPlaceEntity.setUpdateBy(user.getId());


        int result=iVeOaMeetPlaceService.addPlace(veOaMeetPlaceEntity);

        if(result == 0){
            return Result.error(500,"删除失败了");
        }
        return Result.OK();
    }

    // 修改场地
    @AutoLog(value = "会议场地管理-修改场地")
    @ApiOperation(value="修改场地", notes="会议场地管理-修改场地")
    @PostMapping(value = "/amendPlace")
    public Result<?> amendPlace(VeOaMeetPlaceEntity veOaMeetPlaceEntity,Integer state) {
        // 重复判断
        VeOaMeetPlaceEntity entity= iVeOaMeetPlaceService.epetitionrJudge(veOaMeetPlaceEntity.getPlaceName());
        if(state==1){

        }else {
            if(entity!=null){

                return  Result.error(500,"会议场地重复！");

            }
        }

        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        veOaMeetPlaceEntity.setCreateBy(user.getId());
        veOaMeetPlaceEntity.setUpdateBy(user.getId());
        int result=iVeOaMeetPlaceService.amendPlace(veOaMeetPlaceEntity);

        if(result == 0){
            return Result.error(500,"删除失败了");
        }
        return Result.OK();
    }

}
